package com.qianniu.napi.admin.web;

import akka.actor.ActorRef;
import akka.actor.Props;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.*;
import com.qianniu.napi.admin.entity.penum.Qfxtype;
import com.qianniu.napi.admin.entity.penum.Qgettype;
import com.qianniu.napi.admin.entity.penum.Utype;
import com.qianniu.napi.admin.mapper.CouponTplShareMapper;
import com.qianniu.napi.admin.mapper.CoupontplMemberMapper;
import com.qianniu.napi.admin.mapper.LeagueMapper;
import com.qianniu.napi.admin.mapper.LeagueMemberMapper;
import com.qianniu.napi.admin.service.*;
import com.qianniu.napi.admin.service.akka.AkkaServer;
import com.qianniu.napi.admin.service.akka.ResourceActor;
import com.qianniu.napi.common.util.DateUtil;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class LeagueController {

    private static final Logger logger = LoggerFactory.getLogger(LeagueController.class);

    @Autowired
    private LeagueMapper leagueMapper;
    @Autowired
    private CoupontplMemberMapper coupontplMemberMapper;
    @Autowired
    private LeagueMemberMapper leagueMemberMapper;
    @Autowired
    private ILeagueMemberService leagueMemberService;
    @Autowired
    private IRedisService redisService;
    @Autowired
    private IStoreService storeService;
    @Autowired
    private CouponTplShareMapper couponTplShareMapper;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private ICouponService couponService;
    @Autowired
    private ICouponTplService couponTplService;
    @Autowired
    private ICouponTplService coupontplService;
    @Autowired
    private ICouponTplShareService couponTplShareService;

    @RequestMapping("/api/league/infoadd")
    @ResponseBody
    public Jinfo infoadd(League league) {
        Jinfo info = new Jinfo();
        if (ObjectUtil.getUid() == null) {
            info.setCode(1);
            info.setMsg("参数有误");
            return info;
        }

        league.setMid(ObjectUtil.getMid());
        league.setMpid(ObjectUtil.getMpid());
        league.setUid(ObjectUtil.getUid());
        league.setState(1);
        league.setStatus(1);
        league.setStarttime(new Date());

        User user = redisService.getUser();

        if (Strings.isNullOrEmpty(league.getPhone())) league.setPhone(user.getPhone());
        if (Strings.isNullOrEmpty(league.getWxno())) league.setWxno(user.getWeixin());

        league.insertOrUpdate();
        redisService.delUser();
        //更新资源文件
        AkkaServer.system.actorOf(Props.create(ResourceActor.class))
                .tell("League@" + league.getId() + "@update", ActorRef.noSender());

        User nuser = new User();
        nuser.setId(user.getId());
        nuser.setLeagueid(league.getId());
        nuser.setModifytime(new Date());
        nuser.updateById();

        return info;
    }

    @RequestMapping("/api/league/info")
    @ResponseBody
    public Jinfo info() {
        Jinfo info = new Jinfo();
        if (ObjectUtil.getUid() == null) {
            info.setCode(1);
            info.setMsg("参数有误");
            return info;
        }

        Long leagueid = redisService.getUser().getLeagueid();
        if (leagueid == null) return info;

        info.setInfo(leagueMapper.selectById(leagueid));

        return info;
    }

    //用户获取联盟商圈信息
    @RequestMapping("/api/league/userquan")
    @ResponseBody
    public Jinfo userquan(Double latitude, Double longitude, Long lmid) {
        Jinfo info = new Jinfo();
        if (lmid == null) {
            info.setCode(1);
            info.setMsg("参数有误");
            return info;
        }
        //可匿名登陆
        Long uid = ObjectUtil.getUid();
        League league = null;

        league = leagueMapper.selectById(lmid);
        if (league == null) {
            info.setCode(1);
            info.setMsg("联盟商圈不存在");
            return info;
        }

        info.setInfo(league);
        //获取商圈
        if (latitude != null && longitude != null) {
            List<Store> list = storeService.getNearbyLmStores(latitude, longitude, lmid);
            info.setData(list);
        }

        //查询是否领取
        if (uid != null) {
            LeagueMember entity = new LeagueMember();
            entity.setUid(uid);
            List<LeagueMember> leagueMembers = leagueMemberMapper.selectList(
                    new EntityWrapper<LeagueMember>().eq("leagueid", lmid).eq("uid", uid).ge("mlevel", 0));
            info.setExtra(leagueMembers == null || leagueMembers.size() == 0 ? null : leagueMembers.get(0));
        }
        return info;
    }

    //盟主获取联盟商圈信息
    @RequestMapping("/api/league/agentsquan")
    @ResponseBody
    public Jinfo agentsquan(Double latitude, Double longitude) {
        Jinfo info = new Jinfo();
        Long uid = ObjectUtil.getUid();
        if (uid == null) {
            info.setCode(1);
            info.setMsg("请授权登录之后再访问");
            return info;
        }

        Long leagueid = redisService.getUser().getLeagueid();
        if (leagueid == null) {
            info.setCode(1);
            info.setMsg("联盟商圈不存在");
            return info;
        }

        League league = leagueMapper.selectById(leagueid);

        if (league == null) {
            info.setCode(1);
            info.setMsg("联盟商圈不存在");
            return info;
        }
        //获取商圈
        if (latitude != null && longitude != null) {
            List<Store> list = storeService.getNearbyLmStores(latitude, longitude, league.getId());
            info.setData(list);
        }
        info.setInfo(league);

        return info;
    }

    //领取联盟优惠信息
    //可重复领取，每次重复领取，延期一个月会员，并且补上新入驻的商家会员
    @RequestMapping("/api/league/lingqu")
    @ResponseBody
    public Jinfo lingqu(Long lmid, String ori, String scene) {
        Jinfo info = new Jinfo();
        Long uid = ObjectUtil.getUid();
        if (uid == null) {
            info.setCode(1);
            info.setMsg("请授权登录之后再访问");
            return info;
        }

        Long shareid = null;
        String sharename = null;
        //分享来源
        logger.debug("--------------------------------------lingqu ori=" + ori);
        logger.debug("--------------------------------------lingqu scene=" + scene);

        if (!Strings.isNullOrEmpty(scene) || !Strings.isNullOrEmpty(ori)) {
            ori = scene + "@" + ori;
            shareid = ObjectUtil.orderFanuid(ori);
        }
        if (shareid != null) {
            User user = redisService.getUser(shareid);
            sharename = user.getNickName();
        }
        logger.debug("--------------------------------------lingqu shareid=" + shareid);
        logger.debug("--------------------------------------lingqu sharename=" + sharename);
        //获取商圈商家
        List<Store> list = storeService.selectList(new EntityWrapper<Store>()
                .eq("leagueid", lmid).isNotNull("youhuiid"));

        for (Store item : list) {
            CouponTpl coupontpl = coupontplService.selectById(item.getYouhuiid());
            if (coupontpl == null) continue;
            LeagueMember lmentity = new LeagueMember();
            lmentity.setUid(uid);
//            lmentity.setState(1);
            lmentity.setSid(item.getId());
            //领取联盟商家会员信息
            LeagueMember leagueMember = leagueMemberMapper.selectOne(lmentity);
            if (leagueMember == null || leagueMember.getMlevel() < 0) {
                if (leagueMember == null) {
                    leagueMember = new LeagueMember();
                    leagueMember.setUid(uid);
                    leagueMember.setMid(ObjectUtil.getMid());
                    leagueMember.setMpid(ObjectUtil.getMpid());

                    leagueMember.setUtype(Utype.youke.getVal());//领取联盟商圈默认0级：使用会员1个月，
                    leagueMember.setCreator(uid);
                    leagueMember.setLeagueid(lmid);
                    leagueMember.setCreateday(DateUtil.getToday());

                    leagueMember.setCreatetime(new Date());
                    leagueMember.setStartdate(new Date());
                    leagueMember.setEnddate(DateUtil.addDay(new Date(), 30));
                }

                leagueMember.setShareid(shareid);
                leagueMember.setSharename(sharename);
                leagueMember.setLmshareid(shareid);
                leagueMember.setLmsharename(sharename);

                leagueMember.setState(1);
                leagueMember.setMlevel(0);//领取联盟商圈默认0级：使用会员1个月，
                leagueMember.setUtype(0);//用户类型@0：游客；1：购买客户；2：铁杆粉丝

                leagueMember.setSid(item.getId());
                leagueMember.setSname(item.getName());

                leagueMember.setZhekou(coupontpl.getCost());
                leagueMember.setZkname(coupontpl.toNameString());
                leagueMember.setNickname(redisService.getUser().getNickName());
                leagueMember.setPhone(redisService.getUser().getPhone());
                leagueMember.setHeadimg(redisService.getUser().getAvatarPic());

                leagueMember.insertOrUpdate();
            } else {
                if (leagueMember.getState() == 1) continue;
                //第二次领取，再延续一个月体验会员
                leagueMember.setEnddate(DateUtil.addDay(new Date(), 30));
                leagueMember.setState(1);
//                leagueMember.setMlevel(0);//领取联盟商圈默认0级：使用会员1个月，
                leagueMember.setZhekou(coupontpl.getCost());
                leagueMember.setZkname(coupontpl.toNameString());
                leagueMember.updateById();
            }

            //赠送联盟商家试用会员优惠券
            couponService.lingQuan("", null, uid, coupontpl, Qgettype.huiyuan);
        }

        return info;
    }

    //领取店铺会员信息
    //领取店铺会员渠道【必须是店主或者员工或者级别大于等于1级的该店铺会员】
    //会员如果想要延期会员  必须添加店主微信号操作，所以会员期限可以设置三个月、六个月、一年
    //默认领取店铺1级会员资格权益
    //第二年会员满期之后，会员级别自动重置到1级
    @RequestMapping("/api/mchhy/lingqu")
    @ResponseBody
    public Jinfo mchhylingqu(Long sid, String ori, String scene, Integer ismch) {
        Jinfo info = new Jinfo();
        Long uid = ObjectUtil.getUid();
        Long mpid = ObjectUtil.getMpid();
        if (ismch == null) ismch = 0;
        if (uid == null) {
            info.setCode(1);
            info.setMsg("请授权登录之后再访问");
            return info;
        }
        if (sid == null) {
            info.setCode(1);
            info.setMsg("参数有误");
            return info;
        }

        Long shareid = null;
        String sharename = null;
        //分享来源
        logger.debug("--------------------------------------lingqu ori=" + ori);
        logger.debug("--------------------------------------lingqu scene=" + scene);

        if (!Strings.isNullOrEmpty(scene) || !Strings.isNullOrEmpty(ori)) {
            ori = scene + "@" + ori;
            shareid = ObjectUtil.orderFanuid(ori);
        }
        if (shareid != null) {
            User user = redisService.getUser(shareid);
            sharename = user.getNickName();
        }
        Long zfuid = shareid;
        logger.debug("--------------------------------------lingqu shareid=" + shareid);
        logger.debug("--------------------------------------lingqu sharename=" + sharename);

        int cout = leagueMemberService.selectCount(new EntityWrapper<LeagueMember>()
                .eq("sid", sid).ge("mlevel", 1).eq("uid", uid));
        if (cout > 0) {
            info.setCode(0);
            info.setMsg("已领取过会员权益");
            return info;
        }

        LeagueMember lmentity = new LeagueMember();
        lmentity.setUid(uid);
        lmentity.setSid(sid);

        //领取商家裂变一级会员信息
        LeagueMember leagueMember = leagueMemberMapper.selectOne(lmentity);

        //默认领取激活为1级会员
        List<CoupontplMember> mmbquans =
                coupontplMemberMapper.selectList(new EntityWrapper<CoupontplMember>().eq("sid", sid).eq("mlevel", 1));
        if (mmbquans == null || mmbquans.size() == 0) {
            info.setCode(1);
            info.setMsg("商家未设置会员权益");
            return info;
        }

        CouponTpl ctpl = coupontplService.selectById(mmbquans.get(0).getTplid());

        CoupontplMember coupontplMemberentity = new CoupontplMember();
        coupontplMemberentity.setSid(ctpl.getSid());
        coupontplMemberentity.setMlevel(1);
        CoupontplMember tocoupontplMember = coupontplMemberMapper.selectOne(coupontplMemberentity);

        boolean xinke = leagueMember == null;
        if (tocoupontplMember!=null && leagueMember == null || leagueMember.getMlevel() < 1) {
            if (leagueMember == null) {
                leagueMember = new LeagueMember();

                leagueMember.setSname(redisService.getStore(sid).getName());
                leagueMember.setSid(sid);
                leagueMember.setUid(uid);
                leagueMember.setMid(ObjectUtil.getMid());
                leagueMember.setMpid(ObjectUtil.getMpid());
                leagueMember.setIsstaff(ismch);
                leagueMember.setLeagueid(ctpl.getLeagueid());

                leagueMember.setCreator(uid);

                leagueMember.setCreatetime(new Date());
                leagueMember.setStartdate(new Date());
                leagueMember.setEnddate(DateUtil.addDay(new Date(), tocoupontplMember.getExpire()));

                leagueMember.setCreateday(DateUtil.getToday());
                leagueMember.setUtype(Utype.youke.getVal());//领取联盟商圈默认0级：使用会员1个月，//用户类型@0：游客；1：购买客户；2：铁杆粉丝
                leagueMember.setState(1);

                leagueMember.setNickname(redisService.getUser().getNickName());
                leagueMember.setPhone(redisService.getUser().getPhone());
                leagueMember.setHeadimg(redisService.getUser().getAvatarPic());

            } else {
                //若是从体验会员升级，则自动延续一年会员期限（根据1级会员权益设置）
                if (leagueMember.getEnddate() == null) {
                    leagueMember.setStartdate(new Date());
                    leagueMember.setStartday(DateUtil.getToday());
                    leagueMember.setEnddate(DateUtil.addDay(new Date(), tocoupontplMember.getExpire()));
                } else {
                    leagueMember.setEnddate(DateUtil.addDay(leagueMember.getEnddate(), ctpl.getExpire()));
                }

                //此处不改变推荐者人关系（商家推广人关系优先级更高）
                //联盟盟主负责所有商家优惠推广
                //店主负责该商家自有多级会员的推广

            }

            //成为联盟会员lmshareid
            //推荐成为店铺会员lmshareid
            leagueMember.setShareid(shareid);
            leagueMember.setSharename(sharename);

            //寻找级粉丝关系
            lmentity = new LeagueMember();
            lmentity.setUid(shareid);
            lmentity.setSid(sid);

            //领取商家裂变一级会员信息
            LeagueMember ppleagueMember = leagueMemberMapper.selectOne(lmentity);
            leagueMember.setPshareid(ppleagueMember==null?null:ppleagueMember.getShareid());

            leagueMember.setMlevel(1);//领取商家会员默认1级：期限为会员期限，
            leagueMember.setZhekou(ctpl.getCost());
            leagueMember.setZkname(ctpl.toNameString());

            leagueMember.insertOrUpdate();

            //=========待实现，触发推荐者升级=========
            lmentity.setUid(shareid);
            lmentity.setSid(sid);

            //转发者是会员才自动升级看，否则不做升级处理
            //更新华口云领取触发升级<20200914>
            if (mpid == 11) {
                logger.debug("--------------------------------------api/mchhy/lingqu triger update level");

                List<CoupontplMember> cmembers = coupontplMemberMapper.selectList(new EntityWrapper<CoupontplMember>()
                        .setSqlSelect("max(mlevel) mlevel")
                        .eq("sid", ctpl.getSid()));
                logger.debug("-----------------couponRecevie cmembers=" + JSON.toJSONString(cmembers));
                logger.debug("-----------------couponRecevie cmembers.size=" + cmembers.size());
                Integer maxlevel = cmembers.get(0) == null ? null : cmembers.get(0).getMlevel();
                logger.debug("--------------------------------------api/mchhy/lingqu triger update maxlevel="+maxlevel);

                if (ctpl.getSid() != null && ctpl.getFenxiao().intValue() == Qfxtype.hyliebian.getVal() && maxlevel != null) {

                    //================处理转发者================
                    //只计数是往本店铺带新客
                    Wrapper<CouponTplShare> sharesidEntity = new EntityWrapper<CouponTplShare>()
                            .setSqlSelect("distinct getuid ").eq("zfuid", zfuid)
                            .eq("sid", ctpl.getSid())
                            .eq("xinke", 1);

                    List<CouponTplShare> cptlshares = couponTplShareMapper.selectList(sharesidEntity);
                    int sidsharecout = cptlshares == null ? 0 : cptlshares.size();
                    int level = sidsharecout / 3 + 1;

                    level = level > maxlevel.intValue() ? maxlevel.intValue() : level;

                    logger.debug("--------------------------------------api/mchhy/lingqu triger update level="+level);

                    //领取联盟会员之后默认是试用会员等级0，推荐3人升1级，推荐6人升2级，推荐9人升3级。。。
                    //比较当前会员等级
                    LeagueMember zfentity = new LeagueMember();
                    zfentity.setSid(ctpl.getSid());
                    zfentity.setUid(zfuid);
                    leagueMember = leagueMemberMapper.selectOne(zfentity);
                    logger.debug("--------------------------------------api/mchhy/lingqu triger update leagueMember.getMlevel()="+leagueMember.getMlevel());

                    //会员存在并且等级比level小
                    if (leagueMember != null && leagueMember.getMlevel() < level) {
                        leagueMember.setMlevel(level);
                        leagueMember.setModifytime(new Date());
                        leagueMember.updateById();
                    }else if(leagueMember.getUtype()==Utype.kehu.getVal()){
                        leagueMember.setUtype(Utype.tiegan.getVal());
                        leagueMember.setModifytime(new Date());
                        leagueMember.updateById();
                    }
                }
            }

//            if (zfleagueMember != null && mpid == 11) {
//                List<CoupontplMember> cmembers = coupontplMemberMapper.selectList(new EntityWrapper<CoupontplMember>()
//                        .setSqlSelect("max(mlevel) mlevel")
//                        .eq("sid", ctpl.getSid()));
//                int maxlevel = cmembers.get(0).getMlevel();
//                int sharecout = leagueMemberService.selectCount(new EntityWrapper<LeagueMember>()
//                        .eq("sid", sid).ge("mlevel", 1).eq("shareid", shareid));
//                //领取联盟会员之后默认是试用会员等级0，推荐联盟会员不升级，推荐商家会员升级
//                //从店主那里获取商家会员默认1级，推荐3人升2级，推荐6人升级3，推荐9人升4级。。。
//                int tolevel = sharecout / 3 + 1;
//                //每推荐三个人成为会员升一级
//                tolevel = tolevel <= maxlevel ? tolevel : maxlevel;
//
//                if (zfleagueMember.getMlevel().intValue() < tolevel) {
//                    shengji = true;
//                    zfleagueMember.setMlevel(tolevel);
//                    zfleagueMember.setModifytime(new Date());
//                    zfleagueMember.insertOrUpdate();
//                }
//            }

        } else {
            info.setCode(1);
            info.setMsg("您已领取过该商家会员权益");
            return info;
        }

        //升级首次领取会员权益
        //不管是否15号之前之后统一发放
        for (CoupontplMember item : mmbquans) {
            //领取者自己
            //赠送商家一级会员优惠券N张
            for (int i = 0; i < item.getNum(); i++) {
                //非转发非赠送券的动作，此处是转发商家会会员权益
                //而发放的会员券是领取成为商家会员之后
                //            couponService.lingQuan("", ismch==1?shareid:null, uid, ctpl, Qgettype.huiyuan);
                couponService.lingQuantongji("", shareid, uid, ctpl, Qgettype.huiyuan, i == 0&&xinke);
            }
        }

        return info;
    }

    //延期联盟|店铺会员
    @RequestMapping("/api/league/hyyanqi")
    @ResponseBody
    public Jinfo hyyanqi(Long sid, Long uid) {
        Jinfo info = new Jinfo();

        User ruser = redisService.getUser();
        if (ruser.getIsMch() != 1 && ruser.getIsAgent() != 1
                && ruser.getIsPlatform() != 1) {
            info.setCode(1);
            info.setMsg("暂无权限操作");
            return info;
        }

        LeagueMember entity = new LeagueMember();
        entity.setUid(uid);
        entity.setSid(sid);
        LeagueMember leagueMember = leagueMemberMapper.selectOne(entity);

        if (leagueMember == null) {
            info.setCode(1);
            info.setMsg("参数有误");
            return info;
        }

        leagueMember.setEnddate(DateUtil.addDay(leagueMember.getEnddate(), 365));
        leagueMember.setModifytime(new Date());
        leagueMember.setModifior(ObjectUtil.getUid());
        leagueMember.updateById();

        return info;
    }


    //会员裂变活动数据统计
    @RequestMapping("/api/league/hylbtongji")
    @ResponseBody
    public Jinfo hylbtongji(Long sid, Integer pageIndex, Integer pageSize) {
        Jinfo info = new Jinfo();

        User ruser = redisService.getUser();
        if (ruser.getIsMch() != 1 && ruser.getIsAgent() != 1
                && ruser.getIsPlatform() != 1) {
            info.setCode(1);
            info.setMsg("暂无权限操作");
            return info;
        }
        if (sid == null) {
            sid = ObjectUtil.getSid();
        }
        if (sid == null) {
            info.setCode(1);
            info.setMsg("参数有误");
            return info;
        }
        //商家裂变活动会员
        if (pageIndex == null) pageIndex = 1;
        if (pageSize == null) pageSize = 20;
        Page page = new Page(pageIndex, pageSize);

        Wrapper<LeagueMember> entity = new EntityWrapper<LeagueMember>()
                .setSqlSelect("uid,nickname,mlevel,yaoqing,lastxf,phone,headimg,utype")
                .eq("sid", sid).ge("mlevel", -1)
                .orderBy("yaoqing", false).orderBy("lastxf", false);
        Page<LeagueMember> shuju = leagueMemberService.selectPage(page, entity);
        List<LeagueMember> mms = shuju.getRecords();
        for (LeagueMember item : mms) {
            //邀请几位好友消费
//            entity = new EntityWrapper<LeagueMember>()
//                    .setSqlSelect("uid,nickname,mlevel,yaoqing,lastxf,phone,headimg")
//                    .eq("sid", sid).eq("utype", 1).eq("shareid", item.getUid());
            item.setYqlist(couponTplShareService.selectList(new EntityWrapper<CouponTplShare>()
                    .setSqlSelect("distinct getuid ")
                    .eq("sid", sid).eq("utype", 1).eq("zfuid", item.getUid())));

        }
        info.setInfo(mms);

        //变活动会员数据统计
        CouponTplShare today = couponTplShareService.selectOne(new EntityWrapper<CouponTplShare>()
                .setSqlSelect(" sum(ishuiyuan) ishuiyuan,sum(1) getuid,sum(isused) isused")
                .eq("sid", sid).eq("iszhuanfa", 0).eq("createday", DateUtil.getToday())
        );

        CouponTplShare all = couponTplShareService.selectOne(new EntityWrapper<CouponTplShare>()
                .setSqlSelect("sum(ishuiyuan) ishuiyuan,sum(1) getuid,sum(isused) isused")
                .eq("sid", sid).eq("iszhuanfa", 0)
        );

        if (today != null) {
            today.setXinke(leagueMemberService.selectCount(new EntityWrapper<LeagueMember>()
                    .eq("sid", sid).eq("createday", DateUtil.getToday())));
        }
        if (all != null) {
            all.setXinke(leagueMemberService.selectCount(new EntityWrapper<LeagueMember>().eq("sid", sid)));
        }

        JSONObject jo = new JSONObject();
        jo.put("today", today == null ? new CouponTplShare() : today);
        jo.put("all", all == null ? new CouponTplShare() : all);
        info.setExtra(jo);

        return info;
    }

    //会员裂变活动用户数据裂变
    @RequestMapping("/api/league/hylbmms")
    @ResponseBody
    public Jinfo hylbmms(Long sid, Integer pageIndex, Integer pageSize) {
        Jinfo info = new Jinfo();

        User ruser = redisService.getUser();
        if (ruser.getIsMch() != 1 && ruser.getIsAgent() != 1
                && ruser.getIsPlatform() != 1) {
            info.setCode(1);
            info.setMsg("暂无权限操作");
            return info;
        }
        if (sid == null) {
            sid = ObjectUtil.getSid();
        }
        if (sid == null) {
            info.setCode(1);
            info.setMsg("参数有误");
            return info;
        }
        //商家裂变活动会员
        if (pageIndex == null) pageIndex = 1;
        if (pageSize == null) pageSize = 20;
        Page page = new Page(pageIndex, pageSize);

        Wrapper<LeagueMember> entity = new EntityWrapper<LeagueMember>()
                .setSqlSelect("uid,nickname,mlevel,yaoqing,lastxf,phone,headimg,utype")
                .eq("sid", sid).ge("mlevel", -1)
                .orderBy("yaoqing", false).orderBy("lastxf", false);
        Page<LeagueMember> shuju = leagueMemberService.selectPage(page, entity);
        List<LeagueMember> mms = shuju.getRecords();
        for (LeagueMember item : mms) {
            //邀请几位好友消费
//            entity = new EntityWrapper<LeagueMember>()
//                    .setSqlSelect("uid,nickname,mlevel,yaoqing,lastxf,phone,headimg")
//                    .eq("sid", sid).ge("utype", 1).eq("shareid", item.getUid());
            item.setYqlist(couponTplShareService.selectList(new EntityWrapper<CouponTplShare>()
                    .setSqlSelect("distinct getuid ")
                    .eq("sid", sid).eq("utype", 1).eq("zfuid", item.getUid())));
        }
        info.setInfo(mms);

        return info;
    }

}
